<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column prop="shop_id" align="center" label="店铺ID"/>

      <el-table-column prop="shop_name" align="center" label="店铺名称"/>

      <el-table-column prop="realName" align="center" label="实际开店人">
        <template slot-scope="scope">
          {{ scope.row.nick_name + ' - ' + scope.row.username }}
        </template>
      </el-table-column>

      <el-table-column prop="action" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-share"
            @click="handleMove(scope.row)"
            v-hasPermi="['union:shopExamine:move']"
          >
            转移店铺
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog append-to-body width="800px" title="转移店铺" :visible.sync="open">
      <el-form ref="form" :model="form" label-width="180px">
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" readonly/>
        </el-form-item>

        <el-form-item label="转移给" prop="newUserId">
          <el-select
            v-model="form.newUserId"
            laceholder="请选择运营人员"
            clearable
            filterable
            remote
            reserve-keyword
            :remote-method="getOperationUserList"
          >
            <el-option
              v-for="item in operationUserList"
              :key="item.user_id"
              :label="item.operation_name + ' - 第' + item.level + '级 - ' + item.real_name"
              :value="item.user_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOperationUserList, noOperationList, changeShopOperation } from '@/api/union/shop'

export default {
  name: 'noOperation',

  data() {
    return {
      loading: false,
      total: 0,
      showSearch: false,
      queryParams: {
        pageSize: 10,
        pageNum: 1,
        orderByColumn: 'create_time',
        isAsc: 'desc'
      },
      list: [],
      open: false,
      form: {
        shopName: null,
        shopId: null,
        newUserId: null
      },
      operationUserList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      noOperationList().then(response => {
        this.total = response.total
        this.list = response.rows

        this.loading = false
      })
    },
    handleMove(row) {
      this.reset()

      this.open = true
      this.form.shopName = row.shop_name
      this.form.shopId = row.shop_id
    },
    getOperationUserList(query) {
      if (query !== '') {
        setTimeout(() => {
          getOperationUserList(query).then(response => {
            this.operationUserList = response.data
          })
        }, 200)
      }
    },
    // 表单重置
    reset() {
      this.form = {
        shopName: null,
        shopId: null,
        newUserId: null
      }
      this.operationUserList = []
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          changeShopOperation(this.form).then(response => {
            this.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
